package com.cidp.system.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.extremecomponents.table.limit.Limit;

import com.cidp.system.model.Toperator;
import com.cidp.system.model.ToperatorDetail;
import com.cidp.system.model.Trole;
import com.cidp.system.pojo.LoginPo;
import com.cidp.system.pojo.OperatorPo;
import com.cidp.system.pojo.RolePo;
import com.cidp.system.service.IOperatorDetailService;
import com.cidp.system.service.IOperatorService;
import com.cidp.system.service.IRoleService;
import com.litt.core.common.Constants;
import com.litt.core.common.Utility;
import com.litt.core.page.IPageList;
import com.litt.core.page.PagePoParam;
import com.litt.core.util.ExtremeTableUtil;
/**
 * <b>鏍囬锛�</b>浜哄憳绠＄悊action绫�.
 * <pre><b>鎻忚堪</b>
 *    鎿嶄綔鍛樼鐞�
 * </pre>
 * 
 * @author zhanglei 
 * @since 2008-6-6
 * @version 1.0
 */
public class OperatorAction extends DispatchAction{
	private IOperatorService operatorService;
	private IOperatorDetailService operatorDetailService;
	
	/**
	 * 鏌ヨ
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward search(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		OperatorPo operatorPo=(OperatorPo)dynaActionForm.get("operatorPo");
		Limit limit=ExtremeTableUtil.getLimit(request,Constants.DEFAULT_PAGE_SIZE);
		IPageList page = operatorService.listByPage(new PagePoParam(operatorPo, limit.getPage(), limit.getCurrentRowsDisplayed()));
		request.setAttribute("resultList", page.getRsList());
		request.setAttribute("totalRows", new Integer(page.getTotalSize()));
		return mapping.findForward("operatorList");
	}
	/**
	 * 鏌ヨ
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward searchrole(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		OperatorPo operatorPo=(OperatorPo)dynaActionForm.get("operatorPo");
		Limit limit=ExtremeTableUtil.getLimit(request,Constants.DEFAULT_PAGE_SIZE);
		IPageList page = operatorService.listByPage(new PagePoParam(operatorPo, limit.getPage(), limit.getCurrentRowsDisplayed()));
		request.setAttribute("resultList", page.getRsList());
		request.setAttribute("totalRows", new Integer(page.getTotalSize()));
		return mapping.findForward("operatorroleList");
	}
	/**
	 * 澧炲姞
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		return mapping.findForward("operatorAdd");
	}
	/**
	 * 淇濆瓨
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward save(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		OperatorPo operatorPo=(OperatorPo)dynaActionForm.get("operatorPo");
		Toperator operator=new Toperator();
		operator.setLoginId(operatorPo.getLoginId());
		operator.setOpName(operatorPo.getOpName());
		operator.setPassword(Utility.MD5EncodeString(operatorPo.getPassword()));
		operator.setOpCode(operatorPo.getOpCode());
		operator.setSex(operatorPo.getSex());
		operator.setDepartId(operatorPo.getDepartId());
		operator.setMobile(operatorPo.getMobile());
		operator.setEmail(operatorPo.getEmail());
		operator.setIsOnline(new Long(0));
		operator.setLastLoginDatetime(operatorPo.getLastLoginDatetime());
		operator.setLastLoginIp(operatorPo.getLastLoginIp());
		operator.setStatus(operatorPo.getStatus());
		operator.setRemark(operatorPo.getRemark());
		Long strId=operatorService.save(operator);
		ToperatorDetail operatorDetail=new ToperatorDetail();
		operatorDetail.setOpId(strId);
		operatorDetail.setLoginId(operatorPo.getLoginId());
		operatorDetailService.save(operatorDetail);
		return mapping.findForward("success");
	}
	/**
	 * 绶ㄨ集
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward edit(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		OperatorPo operatorPo=(OperatorPo)dynaActionForm.get("operatorPo");
		String sId=request.getParameter("id");
		Long id=new Long(sId);
		Toperator operator=operatorService.load(id);
		BeanUtils.copyProperties(operatorPo,operator);
		dynaActionForm.set("operatorPo", operatorPo);
		return mapping.findForward("operatorEdit");
	}
	/**
	 * 鏌ョ湅
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward watch(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		OperatorPo operatorPo=(OperatorPo)dynaActionForm.get("operatorPo");
		String sId=request.getParameter("id");
		Long id=new Long(sId);
		Toperator operator=operatorService.load(id);
		BeanUtils.copyProperties(operatorPo,operator);
		dynaActionForm.set("operatorPo", operatorPo);
		return mapping.findForward("operatorWatch");
	}
	/**
	 * 淇敼
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		OperatorPo operatorPo=(OperatorPo)dynaActionForm.get("operatorPo");
		Toperator operator=operatorService.load(operatorPo.getOpId());
		operator.setLoginId(operatorPo.getLoginId());
		operator.setOpName(operatorPo.getOpName());
		if(operatorPo.getPassword()!=null && !operatorPo.getPassword().trim().equals("")){
			operator.setPassword(Utility.MD5EncodeString(operatorPo.getPassword()));
		}
		operator.setOpCode(operatorPo.getOpCode());
		operator.setSex(operatorPo.getSex());
		operator.setDepartId(operatorPo.getDepartId());
		operator.setMobile(operatorPo.getMobile());
		operator.setEmail(operatorPo.getEmail());
		operator.setIsOnline(operatorPo.getIsOnline());
		operator.setStatus(operatorPo.getStatus());
		operator.setRemark(operatorPo.getRemark());
		operatorService.update(operator);
		return mapping.findForward("success");
	}
	public ActionForward login(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		String username=(String)dynaActionForm.get("username");
		String password=(String)dynaActionForm.get("password");
		if(operatorService.doIsLongId(username,password)==true){
			List rightList=operatorService.loadRight(username);
			LoginPo loginPo=new LoginPo();
			loginPo.setLoginId(username);
			loginPo.setRightList(rightList);
			HttpSession session=request.getSession();
			session.setAttribute("loginOper", loginPo);
			Toperator operator=operatorService.loadByLoginId(username);
			operator.setIsOnline(new Long(1));
			operator.setLastLoginDatetime(new Date());
			operator.setLastLoginIp(request.getRemoteAddr());
			operatorService.update(operator);
			return mapping.findForward("main");
		}
		return mapping.findForward("login");
		
	}
	public void setOperatorService(IOperatorService operatorService) {
		this.operatorService = operatorService;
	}
	public void setOperatorDetailService(
			IOperatorDetailService operatorDetailService) {
		this.operatorDetailService = operatorDetailService;
	}
}
